package handler

import (
	"context"
	"coroutine/server/basic/config"
	"coroutine/server/basic/model"
	pb "coroutine/server/proto"
)

func (s *Server) AddOrder(_ context.Context, in *pb.AddOrderReq) (*pb.AddOrderResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.AddOrderResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	if u.UIdCard == "" {
		return &pb.AddOrderResp{
			Message: "该用户未实名,不允许购买",
			Code:    10002,
		}, nil
	}
	var t model.Ticket
	config.DB.Debug().Where("id = ?", in.TId).Limit(1).Find(&t)
	if t.Id == 0 {
		return &pb.AddOrderResp{
			Message: "该车次不存在",
			Code:    10003,
		}, nil
	}
	if t.TicketState == "无座" {
		return &pb.AddOrderResp{
			Message: "该车次已无座",
			Code:    10004,
		}, nil
	}
	if t.TicketNumber < in.ONum {
		return &pb.AddOrderResp{
			Message: "车票库存不足",
			Code:    10005,
		}, nil
	}
	u2 := t.TicketNumber - in.ONum
	ticket := model.Ticket{
		Id:           t.Id,
		TicketNumber: u2,
	}
	config.DB.Debug().Updates(&ticket)
	OrderPrice := float64(in.ONum) * t.TicketPrice
	order := model.Order{
		UId:               in.Uid,
		UName:             u.Name,
		CardType:          u.UType,
		UIdCard:           u.UIdCard,
		TId:               in.TId,
		TicketType:        t.TicketType,
		TStart:            t.TicketStart,
		TicketTrainNumber: t.TicketTrainNumber,
		TicketSeatType:    t.TicketSeatType,
		ONum:              in.ONum,
		Price:             OrderPrice,
		OStatus:           in.OStatus,
	}
	err := config.DB.Debug().Create(&order).Error
	if err != nil {
		return &pb.AddOrderResp{
			Message: "订单生成失败",
			Code:    10006,
		}, nil
	}
	return &pb.AddOrderResp{
		Message: "订单生成成功",
		Code:    200,
	}, nil
}

func (s *Server) QueryOrder(_ context.Context, in *pb.QueryOrderReq) (*pb.QueryOrderResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.QueryOrderResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	if u.UIdCard == "" {
		return &pb.QueryOrderResp{
			Message: "该用户未实名,不允许购买",
			Code:    10002,
		}, nil
	}
	var t []model.Ticket
	err := config.DB.Debug().Where("ticket_train_number = ?", in.TicketTrainNumber).Find(&t).Error
	if err != nil {
		return &pb.QueryOrderResp{
			Message: "查询失败",
			Code:    10003,
			List:    nil,
		}, nil
	}
	var query []*pb.QueryTickerList
	for _, ticket := range t {
		query = append(query, &pb.QueryTickerList{
			TicketType:        ticket.TicketType,
			TicketStart:       ticket.TicketStart,
			TicketState:       ticket.TicketState,
			TicketTrainNumber: ticket.TicketTrainNumber,
			TicketSeatType:    ticket.TicketSeatType,
			TicketNumber:      int64(ticket.TicketNumber),
			TicketPrice:       float32(ticket.TicketPrice),
			TicketIdCard:      ticket.TicketIdCard,
		})
	}
	return &pb.QueryOrderResp{
		Message: "查询成功",
		Code:    200,
		List:    query,
	}, nil
}
func (s *Server) QueryOrderType(_ context.Context, in *pb.QueryOrderTypeReq) (*pb.QueryOrderTypeResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.QueryOrderTypeResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	if u.UIdCard == "" {
		return &pb.QueryOrderTypeResp{
			Message: "该用户未实名,不允许购买",
			Code:    10002,
		}, nil
	}
	var t []model.Ticket
	err := config.DB.Debug().Where("ticket_type = ?", in.TicketType).Find(&t).Error
	if err != nil {
		return &pb.QueryOrderTypeResp{
			Message: "查询失败 ",
			Code:    10003,
		}, nil
	}
	var query []*pb.QueryTickerTypeList
	for _, ticket := range t {
		query = append(query, &pb.QueryTickerTypeList{
			TicketType:        ticket.TicketType,
			TicketStart:       ticket.TicketStart,
			TicketState:       ticket.TicketState,
			TicketTrainNumber: ticket.TicketTrainNumber,
			TicketSeatType:    ticket.TicketSeatType,
			TicketNumber:      int64(ticket.TicketNumber),
			TicketPrice:       float32(ticket.TicketPrice),
			TicketIdCard:      ticket.TicketIdCard,
		})
	}
	return &pb.QueryOrderTypeResp{
		Message: "查询成功",
		Code:    200,
		List:    query,
	}, nil
}
func (s *Server) QueryOrderId(_ context.Context, in *pb.QueryOrderIdReq) (*pb.QueryOrderIdResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.QueryOrderIdResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	if u.UIdCard == "" {
		return &pb.QueryOrderIdResp{
			Message: "该用户未实名,不允许购买",
			Code:    10002,
		}, nil
	}
	var t []model.Ticket
	err := config.DB.Debug().Where("id = ?", in.OId).Find(&t).Error
	if err != nil {
		return &pb.QueryOrderIdResp{
			Message: "查询失败 ",
			Code:    10003,
		}, nil
	}
	var query []*pb.QueryTickerIdList
	for _, ticket := range t {
		query = append(query, &pb.QueryTickerIdList{
			TicketType:        ticket.TicketType,
			TicketStart:       ticket.TicketStart,
			TicketState:       ticket.TicketState,
			TicketTrainNumber: ticket.TicketTrainNumber,
			TicketSeatType:    ticket.TicketSeatType,
			TicketNumber:      int64(ticket.TicketNumber),
			TicketPrice:       float32(ticket.TicketPrice),
			TicketIdCard:      ticket.TicketIdCard,
		})
	}
	return &pb.QueryOrderIdResp{
		Message: "查询成功",
		Code:    200,
		List:    query,
	}, nil
}
